package com.itwn.comic.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Role;
import com.itwn.comic.mapper.RoleMapper;
import com.itwn.comic.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleService {
    @Autowired
    RoleMapper roleMapper;
    public PageInfo<Role> queryRoleByPage(Integer size, Integer now, String name) {
        PageHelper.startPage(now,size);
        List<Role> roles = roleMapper.selectRoleByName(name);
        return new PageInfo<>(roles);
    }

    public int addRole(Role role) {
        return roleMapper.insert(role);
    }
    @Transactional
    public void modifyPer(RolePermissionVo roleVo) {
        roleMapper.deleteByRoleID(roleVo.getId());
        String checkedIds = roleVo.getRoleIds();
        List<Integer> list = new ArrayList<>();
        for (String s:checkedIds.split(",")) {
            list.add(Integer.parseInt(s));
        }
        roleMapper.insertRoleAndMenu(roleVo.getId(),list);
    }

    @Transactional
    public void moveRole(Long id) {
        roleMapper.deleteByRoleID(id);
        roleMapper.deleteByPrimaryKey(id);
    }

    public void modify(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    public List<Role> queryAllRoles(){
        return this.roleMapper.selectAll();
    }
}
